<?php
include("wiiavie/connexion/Mysql.php");
include("wiiavie/connexion/SQLRequest.php");
include("vo/value/UserVO.php");
include("vo/value/MediaVO.php");
/**
 * Class ConnexionService
 */ 
class ConnexionService
{

 		function ConnexionService()
 		{
			$this->methodTable["getSessionInfo"]['access'] = 'remote';
       		$this->methodTable["getSessionInfo"]['description'] = 'Return the session info';
			
			$this->methodTable["connectUser"]['access'] = 'remote';
       		$this->methodTable["connectUser"]['description'] = 'Return the user info, provided that login and mdp exist';
			$this->methodTable["connectUser"]['arguments']['login']="";
			$this->methodTable["connectUser"]['arguments']['mdp']="";
			
			$this->methodTable["getMedia"]['access'] = 'remote';
			$this->methodTable["getMedia"]['description'] = 'Return media type & url according to provided user id';
			$this->methodTable["getMedia"]['arguments']['espaceId']="";
		 }
		 
		 public function getSessionInfo()
		 {
		 		$result=array();
				$user = array( 
							"userId"=>"0",
							"espaceId"=>"0",
							"name"=>"",
							"surname"=>"",
							"loggedIn"=>"false",
							"userType"=>"0"
							);
				$userInfo = new UserVO($user);
				array_push($result, $userInfo);
				return $result;
		 }
		 
		 public function connectUser( $login, $mdp )
		 {
		 		$res=array();
				
					$db = Mysql::getInstance( 'localhost', 'viavie', 'root', '' );
					$db->Open();
					
					$request = "SELECT * FROM utilisateur WHERE `mail_user`='".$login."' AND `mdp_user`='".MD5($mdp)."' LIMIT 1";
					$result = $db->Query( $request );
					
					if(mysql_num_rows( $result ) == "0" ){
						$user = array( 
									"userId"=>"-1",
									"espaceId"=>"0",
									"name"=>"",
									"surname"=>"",
									"loggedIn"=>"false",
									"userType"=>"0"
									);
						$userInfo = new UserVO($user);
						array_push($res, $userInfo);
					}else{
						while( $data = mysql_fetch_assoc( $result ) )
				        {
				        	$request = "SELECT * FROM gerer WHERE `id_user`='".$data['id_user']."' LIMIT 1";
							$result = $db->Query( $request );
							while( $data2 = mysql_fetch_assoc( $result ) )
				        	{
				        		$espace_id = $data2['id_espace'];
							}
				            $id = $data['id_user'];
							
				            $prenom = $data['prenom_user'];
				            $nom = $data['nom_user'];
							$type = $data['id_typeUser'];
	
							$user = array( 
										"userId"=>$id,
										"espaceId"=>$espace_id,
										"name"=>$prenom,
										"surname"=>$nom,
										"loggedIn"=>"true",
										"userType"=>$type
										);
							$userInfo = new UserVO($user);
							array_push($res, $userInfo);
				        }
					}
					$db->Close(); 

				return $res;
		 }
	 
	 public function getMedia( $espaceId )
	 {
	 
	 	$res = array();
		
		$db = Mysql::getInstance( '127.0.0.1', 'viavie', 'root', '' );
		$db->Open();
		
		$request = "SELECT espace_media.* FROM espace_media WHERE `id_espace` =".$espaceId;
		$result = $db->Query( $request );
		
		while( $data = mysql_fetch_assoc( $result ) )
        {
            $id				= $data['id_espaceMedia'];
            $id_espace 		= $data['id_espace'];
			$chemin			= $data['nom_espaceMedia'];
            $date	 		= $data['date_espaceMedia'];

			$media = array( 
						"id"=>$id,
						"idespace"=>$id_espace,
						"chemin"=>$chemin,
						"date"=>$date
						);
			$mediaInfo = new MediaVO($media);
			array_push($res, $mediaInfo);
		}
		$db->Close(); 

		//return array("id"=>"bonjour","idespace"=>"tu","chemin"=>"vas", "date"=>"bien"); 
		return $res;
	 }
}
?>
